//
//  MM_FileTool.m
//  MallManage_IOS
//
//  Created by 张浩 on 2023/9/19.
//

#import "MM_FileTool.h"

#define  fileManagerDefault  [NSFileManager defaultManager]

@implementation MM_FileTool

/*
 * 获得Documents，Library，Cache 中其中一个类型
 *
 */
+ (NSString *)getDirectoryWithType:(MM_FileType)fileType {
    
    NSUInteger type = NSDocumentDirectory;
    if (fileType == MM_FileTypeDocument) {
        
        type = NSDocumentDirectory;
    }else if (fileType == MM_FileTypeLibrary){
        
        type = NSLibraryDirectory;
    }else{
        
        type = NSCachesDirectory;
    }
    NSArray *paths = NSSearchPathForDirectoriesInDomains(type, NSUserDomainMask, YES);
    NSString *directory = [paths objectAtIndex:0];
    return directory;
}


/*
 * 根据路径
 * 创建文件夹
 */
+ (NSString *)createFileFileDirectory:(NSString *)fileDirectory {
    
    if ([fileManagerDefault fileExistsAtPath:fileDirectory]) {
        //        MMLog(@"文件已存在");
    } else {
        NSError *error = nil;
        BOOL res = [fileManagerDefault createDirectoryAtPath:fileDirectory withIntermediateDirectories:YES attributes:nil error:&error];
        if (res) {
            //            MMLog(@"文件创建成功: %@" ,fileDirectory);
        }else{
            fileDirectory = nil;
            //            MMLog(@"文件创建失败");
        }
    }
    return fileDirectory;
}

/*
 * document的directory中的 Resource 文件
 **/
+ (NSString *)getResourceOfDocumentDirectory {
    
    NSString *doc =[MM_FileTool getDirectoryWithType:MM_FileTypeDocument];
    NSString *resourcePath = [doc stringByAppendingPathComponent:@"MM_App_Resource"];
    return  [MM_FileTool createFileFileDirectory:resourcePath];
}


/**
 *  根据资源类型找到该路径
 *
 *  @param type 资源类型
 *
 *  @return 资源类型的路径
 */
+ (NSString *)pathOfResourceType:(MM_ResourceType)type {
    
    NSString * fileName = [MM_FileTool getResourceOfDocumentDirectory];
    if (fileName.length == 0) {
        return nil;
    }
    if (type == MM_ResourceTypeUserInfo) {
        
        fileName = [fileName stringByAppendingPathComponent:@"MM_UserInfo"];
    }else if (type == MM_ResourceTypeUrl) {
        
        fileName = [fileName stringByAppendingPathComponent:@"MM_Url"];
    }else if (type == MM_ResourceTypePic) {
        
        fileName = [fileName stringByAppendingPathComponent:@"MM_Pic"];
    }else if(type == MM_ResourceTypeVideo){
        
        fileName = [fileName stringByAppendingPathComponent:@"MM_Video"];
    }else if(type == MM_ResourceTypeAudio){
        
        fileName = [fileName stringByAppendingPathComponent:@"MM_Audio"];
    }else if(type == MM_ResourceTypeOther){
        
        fileName = [fileName stringByAppendingPathComponent:@"MM_Other"];
    }
    
    NSString *path =  [MM_FileTool createFileFileDirectory:fileName];
    return path;
}

+ (NSString *)readPathOfResourceType:(MM_ResourceType)type fileName:(NSString *)fileName {
    NSString * path = [MM_FileTool pathOfResourceType:type];
    if (fileName) {
        fileName = [path stringByAppendingPathComponent:fileName];
    }
    return fileName;
}

/**
 *  删除某种资源类型
 *
 *  @param type 资源类型
 *
 *  @return 资源类型的路径
 */
+ (BOOL)removePathOfResourceType:(MM_ResourceType)type {
    
    
    return [self removePath:[MM_FileTool pathOfResourceType:type]];
}


/*
 * 删除所有的资源文件
 **/
+ (BOOL)removeAllCustomPath {
    
    return [self removePath:[MM_FileTool getResourceOfDocumentDirectory]];
}

/* 删除路径**/
+ (BOOL)removePath:(NSString *)path {
    
    BOOL res=[fileManagerDefault removeItemAtPath:path error:nil];
    if (res) {
        MMLog(@"文件删除成功");
    }else {
        MMLog(@"文件删除失败");
    }
    return res;
}


+ (void)removeNSUserDefaultData
{
    NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
    NSDictionary * dict = [defs dictionaryRepresentation];
    for (id key in dict) {
        
        if (![key isEqual:@"isShowedSubAgentGuide"]) {
            [defs removeObjectForKey:key];
        }
    }
    [defs synchronize];
}

/* 计算 某个文件中的 的缓存 **/
+ (CGFloat)getDiskSizeCacheWithPath:(NSString *)diskCachePath {
    
    NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:diskCachePath];
    CGFloat size = 0;
    for (NSString *fileName in fileEnumerator) {
        NSString *filePath = [diskCachePath stringByAppendingPathComponent:fileName];
        NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
        size += [attrs fileSize];
    }
    return size;
}

+ (NSString*)getFilePath {
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *savePath = [paths objectAtIndex:0];
    NSString *subDic = @"File";
    NSString *downloadDic = [NSString stringWithFormat:@"%@/%@", savePath, subDic];
    // 文件下载存放路径
    NSFileManager *fm = [NSFileManager defaultManager];
    if ( ![fm fileExistsAtPath:downloadDic] )
    {
        [fm createDirectoryAtPath:downloadDic withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return downloadDic;
}
@end
